import { NativeHelper } from "../../native/NativeHelper";
import { ILoginAble } from "../ILoginAble";
import { argType } from "../IPublisher";

export class NativeLogin extends ILoginAble{
    protected _className:string = "sdk.{0}.Login";
    
    init(type: string): ILoginAble {
        this._className = this._className.replace("{0}", type);
        return super.init(type);
    }
    
    isSupport(): boolean {
        return NativeHelper.callWithReturnType(this._className, "isSupport", false) as boolean;
    }

    /**
     * 登录平台
     * @param caller  回调this
     * @param onSuccess 成功回调
     * @param onFail 失败回调
     */
    login(caller?:any, onSuccess?:Function, onFail?:Function): void {
        let callbacks:string[] = [];
        callbacks.push(onSuccess != null ? NativeHelper.registerCallbackOnce(caller, onSuccess) : "");
        callbacks.push(onFail != null ? NativeHelper.registerCallbackOnce(caller, onFail) : "");
        NativeHelper.call(this._className, "login", ...callbacks);
    }

    /**
     * 注销账号
     */
    logout(): void {
        NativeHelper.call(this._className, "logOut");
    }

    /**
     * 登录成功的sessionID
     */
    get sessionID(): argType {
        return NativeHelper.callWithReturnType(this._className, "getSessionID", "");
    }
    /**
     * 登录成功的openID
     */
    get openID(): argType {
        return NativeHelper.callWithReturnType(this._className, "getOpenID", "");
    }

    get userInfo(): any {
        return NativeHelper.callWithReturnType(this._className, "getUserInfo", "");
    }
    
}
